using System;
using System.Collections.Generic;
using System.Text;
using IronSvn;
using System.IO;

namespace IronSvn
{
   public class Program
   {
      public static void Main(string[] args)
      {
         Console.Write("Subversion Library Initializing...");
         SvnVersion version = Svn.GetVersion();
         using (SvnClient client = new SvnClient())
         {
            Console.WriteLine("OK\nVersion {0}", version);
            Console.WriteLine("Current directory: {0}\n", Environment.CurrentDirectory);
            PrintHelp();
            Console.WriteLine();
            bool go = true;
            while (go)
            {
               try
               {
                  Console.Write("> ");
                  string line = Console.ReadLine();
                  if (line != null)
                  {
                     line = line.Trim();
                     if (line.Length > 0)
                     {
                        switch (line)
                        {
                           case "x":
                           case "q":
                           case "exit":
                           case "quit":
                              go = false;
                              break;
                           case "help":
                           case "h":
                           case "?":
                           case "usage":
                           case "-help":
                              PrintHelp();
                              break;
                           case "example":
                              Example example = new ExampleFull();
                              example.Test();
                              break;
                           case "htpasswd":
                              Console.Write("File: ");
                              string file = Console.ReadLine();
                              Console.Write("User: ");
                              string user = Console.ReadLine();
                              Console.Write("Password: ");
                              string password = Console.ReadLine();

                              IronSvn.Htpasswd.SetPassword(file, user, password);

                              break;
                           default:
                              ExecuteCommand(line, client);
                              break;
                        }
                     }
                  }
               }
               catch (Exception ex)
               {
#if DEBUG
                  Console.WriteLine("Error: " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine);
#else
                  Console.WriteLine("Error: " + ex.Message + Environment.NewLine);
#endif
               }
            }
         }
      }

      private static void PrintHelp()
      {
         Console.Write(@"usage: (x to exit, h for help menu)
x: exit
h: this help/usage menu
example: run regression test
debug VALUE
svn co URL PATH
svn update PATH
svn commit PATH MESSAGE
user USERNAME
password PASSWORD
");
      }

      private static void ExecuteCommand(string line, SvnClient client)
      {
         string old = line;
         if (line.StartsWith("debug"))
         {
            bool debug = true;
            if (!bool.TryParse(line.Substring(5), out debug))
            {
               debug = true;
            }
            //IronSvn.Svn.IsLogEnabled = debug;
            //Console.WriteLine("Debugging set to {0}", IronSvn.Svn.IsLogEnabled);
            return;
         }
         if (line.StartsWith("svn "))
         {
            line = line.Substring(4);
         }
         if (line.StartsWith("co ") || line.StartsWith("checkout "))
         {
            line = line.Substring(line.IndexOf(' ') + 1);

            string url = line, checkoutPath;

            int spaceIndex = url.IndexOf(' ');
            if (spaceIndex != -1)
            {
               checkoutPath = url.Substring(spaceIndex + 1);

               if (!Directory.Exists(checkoutPath))
               {
                  Directory.CreateDirectory(checkoutPath);
               }

               Environment.CurrentDirectory = checkoutPath;

               url = url.Substring(0, spaceIndex);
            }
            else
            {
               checkoutPath = Environment.CurrentDirectory;
            }

            Console.WriteLine("Checking out {0} to {1}...", url, checkoutPath);
            SvnResult result = client.Checkout(url, checkoutPath);
            Console.WriteLine("Revision: {0}", result.Result);

            return;
         }
         else if (line.StartsWith("update "))
         {
            line = line.Substring(7);

            Console.WriteLine("Updating {0}...", line);
            SvnResult result = client.Update(line);
            Console.WriteLine("Revision: {0}", result.Result);

            return;
         }
         else if (line.StartsWith("commit "))
         {
            string message = "";
            line = line.Substring(7);
            int space = line.IndexOf(' ');
            if (space != -1)
            {
               message = line.Substring(space + 1);
               line = line.Substring(0, space);
            }

            Console.WriteLine("Committing {0}", line);
            SvnResult result = client.Commit(line, message);
            Console.WriteLine("Revision = {0}", result.Result);
            return;
         }
         else if (line.StartsWith("user "))
         {
            client.UserName = line.Substring(5);
            return;
         }
         else if (line.StartsWith("password "))
         {
            client.Password = line.Substring(9);
            return;
         }

         Console.WriteLine("Unknown command " + old);
      }
   }
}
